package com.zzxy.spring.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

// 自定义的BeanPostProcessor，用于在bean初始化前后执行特定逻辑
public class MyBeanPostProcessor implements BeanPostProcessor {

    // 在bean初始化之前调用此方法
    // 参数o是即将被初始化的bean实例，参数s是该bean的名称
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        // 打印bean初始化前的信息
        System.out.println("Bean [" + s + "] is going to be initialized. (Before Initialization)");
        // 返回bean实例
        return o;
    }

    // 在bean初始化之后调用此方法
    // 参数o是已经被初始化的bean实例，参数s是该bean的名称
    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        // 打印bean初始化后的信息
        System.out.println("Bean [" + s + "] has been initialized. (After Initialization)");
        // 返回bean实例
        return o;
    }
}